package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.dao.InsertTempOrderDao;
import com.clqteam.daoimpl.InsertTempOrderDaoImpl;
import com.clqteam.model.Employee;
import com.clqteam.model.Product;
import com.clqteam.serviceimpl.InsertTempOrderServiceImpl;
/**
 * 插入临时订单信息控制层
 * @author ZHX
 *
 */
public class InsertTempOrderController extends HttpServlet{
	
	InsertTempOrderDao itoDao = new InsertTempOrderDaoImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF8");
		resp.setCharacterEncoding("UTF8");
		
		HttpSession session = req.getSession();
		Employee e = (Employee)session.getAttribute("employee");
		int eId = e.geteId();
		String tbName = "s_ResaleOrder_"+eId;
		
		int ptId = Integer.parseInt(req.getParameter("pId"));
		int ptAmount = Integer.parseInt(req.getParameter("pAmount"));	
		
		int ifexist = 0;
		List<Product> pros = itoDao.getExist(tbName);
		for(Product pro:pros){
			if(ptId == pro.getpId()){
				ptAmount = pro.getPoAmount()+ptAmount;
				ifexist = 1;
			}
		}		
		int flag = -1;
		if(ifexist == 1){
			flag = itoDao.updateTempOrderInfo(ptId, ptAmount,tbName);
		}else{
			flag = itoDao.insetTempOrderInfo(ptId, ptAmount,tbName);
		}
		PrintWriter pw = resp.getWriter();
		pw.write(String.valueOf(flag));
		pw.flush();
		pw.close();
		
	}
	
}
